<!--
  - ControllerAsh.php
  - v1.3
  - 4/02/2011
  -->

<!--
  - Code Specifications:
  - This code processes the inputs of the user in ViewAsh.php
  - This also stores the inputs, mean, and standard deviation into the 
  - database table.
  -->
	
<?php
	//Gets required libraries
	require '../libraries/EditAnalysisLib.php';
	require '../libraries/ComputeAnalysisLib.php';
	require '../libraries/UserLibrary.php';
	
	//Formally starts session
	session_start();
	
	//Ensures that the analyst is logged in.
	if(isSessionSet("analyst", NULL, "../Logout.php"));
	
	//Gathers data input from ViewAsh.php
	$weightDish1 =  $_POST['14/1'];
	$weightSample1 = $_POST['15/1'];
	$weightDishAshed1 = $_POST['16/1'];	
	$weightDish2 =  $_POST['14/2'];
	$weightSample2 = $_POST['15/2'];
	$weightDishAshed2 = $_POST['16/2'];	
	
	//Create connection to the database
	$connect = mysql_connect("localhost","editanalysis","editanalysis");	
	if (!$connect) {
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("casl", $connect);
	
	//Execute query
	$val = mysql_query("SELECT * FROM analysis_result WHERE casl_year = 
			$_SESSION[year] AND casl_sample = $_SESSION[no]");
	$row = mysql_fetch_array($val);
	
	//Computes the ash content from the first column of inputs
	if (isset($_POST['compute1'])) {
		$compute1 = ComputeAshContent($weightDish1, $weightSample1,
								$weightDishAshed1);
		$mean = ComputeMean($compute1, $row['result_value2'], 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute1, 
				$row['result_value2'], $mean, 2);
		$edit = editAsh($standardDeviation, $mean, "result_value1", $compute1,
				"value1", $weightDish1, $weightSample1, $weightDishAshed1, 
				$_SESSION["year"], $_SESSION["no"],14, 15, 16, "casl", 
				"localhost", "editanalysis", "editanalysis");
	//Computes the ash content from the second column of inputs
	} else if (isset($_POST['compute2'])) {
		$compute2 = ComputeAshContent($weightDish2, $weightSample2, 
				$weightDishAshed2);
		$mean = ComputeMean($compute2, $row['result_value1'], 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute2, 
				$row['result_value1'], $mean, 2);
		$edit = editAsh($standardDeviation, $mean, "result_value2", $compute2, 
				"value2", $weightDish2, $weightSample2, $weightDishAshed2, 
				$_SESSION["year"], $_SESSION["no"],14, 15, 16, "casl", 
				"localhost", "editanalysis", "editanalysis");
	//Computes the ash content from both columns of inputs
	} else if (isset($_POST['computeBoth'])) {
		$compute1 = ComputeAshContent($weightDish1, $weightSample1,
								$weightDishAshed1);
		$compute2 = ComputeAshContent($weightDish2, $weightSample2, 
								$weightDishAshed2);
		$mean = ComputeMean($compute1, $compute2, 0, 2);
		$standardDeviation = ComputeStandardDeviation($compute1, $compute2, 
				$mean, 2);
		$edit1 = editAsh($standardDeviation, $mean, "result_value1", $compute1,
				"value1", $weightDish1, $weightSample1, $weightDishAshed1, 
				$_SESSION["year"], $_SESSION["no"],14, 15, 16, "casl", 
				"localhost", "editanalysis", "editanalysis");
		$edit2 = editAsh($standardDeviation, $mean, "result_value2", $compute2,
				"value2", $weightDish2, $weightSample2, $weightDishAshed2, 
				$_SESSION["year"], $_SESSION["no"],14, 15, 16, "casl", 
				"localhost", "editanalysis", "editanalysis");
	}
	
	//Redirect back to ViewAsh.php
	header("Location: ViewAsh.php");
?>